package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.domain.SchoolLinkman;
import cn.wolfcode.education.query.ClassroomQuery;
import cn.wolfcode.education.service.ISchoolLinkmanService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/schoolLinkman")
public class SchoolLinkmanController {
    @Autowired
    private ISchoolLinkmanService schoolLinkmanService;

    @RequestMapping("")
    public String index(){
        return "schoollinkman/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(ClassroomQuery qo){
        return schoolLinkmanService.queryForList(qo);
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public List<SchoolLinkman> selectAll(ClassroomQuery qo){
        qo.setRows(1000);
        PageResult pageResult = schoolLinkmanService.queryForList(qo);
        List rows = pageResult.getRows();
        return rows;
    }
    @RequestMapping("/getLinkmanBySchoolId")
    @ResponseBody
    public List<SchoolLinkman> getLinkmanBySchoolId(Long schoolId){
        List<SchoolLinkman> list = schoolLinkmanService.getLinkmanBySchoolId(schoolId);
        return list;
    }

    @RequestMapping("/save")
    @ResponseBody
    public JSONResult save(SchoolLinkman schoolLinkman){
        JSONResult jsonResult = new JSONResult();
        try {
            schoolLinkmanService.insert(schoolLinkman);
        }catch (RuntimeException e){
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }
    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(SchoolLinkman schoolLinkman){
        JSONResult jsonResult = new JSONResult();
        try {
            schoolLinkmanService.update(schoolLinkman);
        }catch (RuntimeException e){
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }
    /**
     * 注意:这个方法是设置主联系人
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id , Long schoolId) {
        JSONResult jsonResult = new JSONResult();
        try {
            schoolLinkmanService.setMainLinkman(id,schoolId);
        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }
}
